<?
    require_once("mvc/action/ActionForm.php");
    require_once("mvc/action/ActionMessage.php");
    require_once("mvc/action/ActionMessages.php");

    require_once("cms/content/om/ContentI18n.php");


    class BaseContentForm extends ActionForm {

        protected $contentI18n;
        protected $day;
        protected $month;
        protected $year;
        protected $formImage;
        protected $deleteImage;

        public function __construct() {
            $this->contentI18n = new ContentI18n();
            $this->day = null;
            $this->month = null;
            $this->year = null;
            $this->formImage = null;
            $this->deleteImage = false;
        }

        public function getContentI18n() {
            return $this->contentI18n;
        }

        public function setContentI18n(ContentI18n $contentI18n) {
            $this->contentI18n = $contentI18n;
        }

        public function getDay() {
            return $this->day;
        }

        public function setDay($day) {
            $this->day = $day;
        }

        public function getMonth() {
            return $this->month;
        }

        public function setMonth($month) {
            $this->month = $month;
        }

        public function getYear() {
            return $this->year;
        }

        public function setYear($year) {
            $this->year = $year;
        }

        public function getFormImage() {
            return $this->formImage;
        }

        public function setFormImage($formImage) {
            $this->formImage = $formImage;
        }

        public function getDeleteImage() {
            return $this->deleteImage;
        }

        public function setDeleteImage($deleteImage) {
            $this->deleteImage = $deleteImage;
        }

        public function validate() {

            $errors = new ActionMessages();

            if(is_null($this->getContentI18n()->getName()) || strlen($this->getContentI18n()->getName()) == 0)
                $errors->add("contentI18n->name",
                    new ActionMessage("errors.required", true, new ActionMessage("prompt.name", true)));


            if(!is_null($this->getContentI18n()->getName()) && strlen($this->getContentI18n()->getName()) > 0 && strlen($this->getContentI18n()->getName()) > 255)
                $errors->add("contentI18n->name",
                    new ActionMessage("errors.maxlength", true, array(
                        new ActionMessage("prompt.name", true),
                        new ActionMessage("255", false))));


            if(is_null($this->getContentI18n()->getBody()) || strlen($this->getContentI18n()->getBody()) == 0)
                $errors->add("contentI18n->body",
                    new ActionMessage("errors.required", true, new ActionMessage("prompt.body", true)));


            if(!is_null($this->getContentI18n()->getContent()->getLink()) && strlen($this->getContentI18n()->getContent()->getLink()) > 0 && !preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $this->getContentI18n()->getContent()->getLink()))
                $errors->add("contentI18n->content->link",
                    new ActionMessage("errors.url", true, new ActionMessage("prompt.link", true)));


            if(!is_null($this->getContentI18n()->getContent()->getLink2()) && strlen($this->getContentI18n()->getContent()->getLink2()) > 0 && !preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $this->getContentI18n()->getContent()->getLink2()))
                $errors->add("contentI18n->content->link2",
                    new ActionMessage("errors.url", true, new ActionMessage("prompt.link2", true)));


            global $ALLOWABLE_IMAGES;
            if(!is_null($this->getFormImage()) && isset($this->formImage["size"]) && $this->formImage["size"] > 0 && !array_key_exists($this->formImage["type"], $ALLOWABLE_IMAGES))
                $errors->add("formImage",
                    new ActionMessage("errors.image", true, new ActionMessage("prompt.image", true)));

            return $errors;
        }

    }
?>